/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.javahispano.javacup.modelo;

/**
 *
 * @author fabnun
 */
public class TrayectoriaAire extends AbstractTrayectoria {

    private final static double k = 0.7, g = 9.8, n = 1.5, ay = 2.3;
    final AbstractTrayectoria nextTrayectory;

    public TrayectoriaAire(double vx0, double vy0, double x0, double y0) {
        super(vx0,vy0,x0,y0);
        x0=getX(t0);
        y0 = getY(t0);
        vx0 = (getX(t0) - getX(t0 - .1))*4;
        vy0 = (getY(t0 - .1) - getY(t0))*3;
        if (vy0>0.1){
            nextTrayectory = new TrayectoriaAire(vx0, vy0, x0, y0);
        } else {
            nextTrayectory = new TrayectoriaSuelo(Math.sqrt(vx0*vx0+vy0*vy0)*2, x0);
        }
    }

    @Override
    public double getX(double t) {
        if (t > t0 && nextTrayectory != null) {
            return nextTrayectory.getX(t-t0);
        } else {
            return vx0 * (1 - Math.exp(-k * n * t)) / k + vx0 * t + x0;
        }
    }

    @Override
    public double getY(double t) {
        if (t > t0 && nextTrayectory != null) {
            return nextTrayectory.getY(t-t0);
        } else {
            return y0 + vy0 * ay * t - g * t * t / 2d;
        }
    }

    @Override
    public double getT0() {
        double b = (vy0 * ay);
        double a = g / 2;
        if (b * b > -4 * a * y0) {
            double ta = (b + Math.sqrt(b * b + 4 * a * y0)) / 2 / a;
            double tb = (b - Math.sqrt(b * b + 4 * a * y0)) / 2 / a;
            return Math.max(ta, tb);
        } else {
            return 0;
        }
    }
}
